﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace ConsoleApplication1Tests
{
    [TestClass]
    public class 递归矩阵
    {
        int[,] Init(int rows, int columns)
        {
            if (rows < 1 || columns < 1)
                return null;

            int[,] arr = new int[rows, columns];

            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < columns; j++)
                {
                    arr[i, j] = i * columns + j + 1;
                }
            }

            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < columns; j++)
                {
                    Console.Write("{0}\t", arr[i, j]);
                }
                Console.WriteLine();
                Console.WriteLine();
            }
            return arr;
        }

        void PrintMatrix(int[,] arr, int rows, int columns, int start = 0)
        {
            if (arr == null || rows < 1 || columns < 1 || start >= rows || start >= columns)
            {
                return;
            }

            int stopX = columns - start - 1;
            int stopY = rows - start - 1;

            for (int i = start; i <= stopX; i++)
            {
                Console.Write("{0}  ", arr[start, i]);
            }

            for (int i = start + 1; i <= stopY; i++)
            {
                Console.Write("{0}  ", arr[i, stopX]);
            }

            for (int i = stopX - 1; i >= start; i--)
            {
                Console.Write("{0}  ", arr[stopY, i]);
            }

            for (int i = stopY - 1; i >= start + 1; i--)
            {
                Console.Write("{0}  ", arr[i, start]);
            }
            PrintMatrix(arr, rows, columns, start + 1);
        }
        [TestMethod]
        public void TestMethod1()
        {
            var arr = Init(1, 1);
            arr = Init(2, 2);
            arr = Init(5, 5);

            Console.WriteLine();
            PrintMatrix(arr, 5, 5);
        }
    }
}
